<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="工艺路线编号" prop="routeCode">
          <el-input
            v-model="queryParams.routeCode"
            placeholder="请输入工艺路线编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="工艺路线名称" prop="routeName">
          <el-input
            v-model="queryParams.routeName"
            placeholder="请输入工艺路线名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="queryParams.remark"
            placeholder="请输入备注"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['mes:pro:proroutebom:add']"
          >新增
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table border v-loading="loading" :data="prorouteList" @selection-change="handleSelectionChange">
        <el-table-column label="工艺路线编号" align="center" prop="routeCode">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleView(scope.row)"
              v-hasPermi="['mes:pro:proroutebom:query']"
            >{{ scope.row.routeCode }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="工艺路线名称" align="center" prop="routeName"/>
        <el-table-column label="工艺路线说明" align="center" prop="routeDesc"/>
        <el-table-column label="是否已确认" align="center" prop="enableFlag">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enableFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="启用状态" align="center" width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enableStatus"
              active-value="Y"
              inactive-value="N"
              @change="handleEnableChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark"/>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-if="scope.row.enableFlag != 'Y'"
              v-hasPermi="['mes:pro:proroutebom:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="handleStatusChange(scope.row)"
              v-if="scope.row.enableFlag != 'Y'"
              v-hasPermi="['mes:pro:proroutebom:edit']"
            >确认
            </el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              size="mini"
              @click="handleDelete(scope.row)"
              v-if="scope.row.enableFlag != 'Y'"
              v-hasPermi="['mes:pro:proroutebom:remove']"
            >删除
            </el-button>
            <el-button
              type="text"
              icon="el-icon-document-copy"
              size="mini"
              @click="handleCopy(scope.row)"
              v-if="scope.row.enableFlag == 'Y'"
              v-hasPermi="['mes:pro:proroutebom:add']"
            >复制
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改工艺路线对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1080px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="编号" prop="routeCode">
              <el-input v-model="form.routeCode" placeholder="请输入工艺路线编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="routeName">
              <el-input v-model="form.routeName" placeholder="请输入工艺路线名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="说明" prop="routeDesc">
              <el-input v-model="form.routeDesc" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="BOM编码" prop="bomCode">
              <el-input v-model="form.bomCode" :disabled="optType == 'edit'" placeholder="请选择BOM编码">
                <el-button
                  slot="append"
                  @click="handleSelectBom"
                  v-if="optType != 'view' && optType != 'edit'"
                  icon="el-icon-search"
                ></el-button>
              </el-input>
              <BomSelect ref="bomSelect" @onSelected="onBomSelected"></BomSelect>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="BOM版本" prop="bomName">
              <el-input v-model="form.bomName" disabled placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="productName">
              <el-input v-model="form.productName" disabled placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图纸版本号" prop="drawingVersion">
              <el-input v-model="form.drawingVersion" disabled placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品图号" prop="attr1">
              <el-input v-model="form.attr1" disabled placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-tabs type="border-card" v-if="form.routeId != null">
        <el-tab-pane label="组成工序">
          <Routeprocess :optType="optType" :routeId="form.routeId" :drawingId='form.drawingId' :routeStatus="form.status" @input="handleInput"></Routeprocess>
        </el-tab-pane>
        <el-tab-pane label="工序物料" :disabled="form.status != 1">
          <el-tabs type="border-card">
            <el-tab-pane v-for="(process,index) in processOptions" :key="index" :label="process.processName">
              <RouteProductBom
                v-if="form.routeId !=null"
                :optType="optType"
                :routeId="form.routeId"
                :productId="form.productId"
                :processId="process.processId"
                :bomId="form.bomId"></RouteProductBom>
            </el-tab-pane>
          </el-tabs>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel" v-if="optType =='view'">返回</el-button>
        <el-button type="primary" @click="submitForm" v-else>确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addProrouteBom, changeEnableStatus, changeRouteStatus, copyProrouteBom, delProrouteBom, getProrouteBom, listProrouteBom, updateProrouteBom } from "@/api/mes/pro/proroute";
import Routeprocess from "./routeprocess";
import { genCode } from "@/api/system/autocode/rule"
import RouteProductBom from "./routeproductbom.vue";
import { listProcess } from "@/api/mes/pro/routeprocess";
import BomSelect from '@/components/mdBomSelectgh/single.vue';
import DrawingSelect from '@/components/mdDrawingSelect/single.vue';
import { listDrawing } from "@/api/mes/md/drawing";

export default {
  name: "Proroutegh",
  dicts: ['sys_yes_no'],
  components: {DrawingSelect, BomSelect, RouteProductBom, Routeprocess},
  data() {
    return {
      showFlag: false,
      productId: null,
      // 工序列表
      processOptions: [],
      //自动生成编码
      autoGenFlag: false,
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工艺路线表格数据
      prorouteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        attr3: 1,
        routeCode: null,
        routeName: null,
        routeDesc: null,
        enableFlag: null,
        remark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        routeCode: [
          {required: true, message: "工艺路线编号不能为空", trigger: "blur"}
        ],
        routeName: [
          {required: true, message: "工艺路线名称不能为空", trigger: "blur"}
        ],
        bomCode: [
          {required: true, message: "BOM编码不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工艺路线列表 */
    getList() {
      this.loading = true;
      listProrouteBom(this.queryParams).then(response => {
        this.prorouteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        routeId: null,
        routeCode: null,
        routeName: null,
        routeDesc: null,
        enableFlag: 'N',
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        bomId: null,
        bomCode: null,
        bomName: null,
        drawingId: null,
        drawingName: null,
        drawingVersion: null,
        productCode: null,
        productName: null,
        attr1: null,
      };
      this.autoGenFlag = false;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.routeId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.handleAutoGenChange(true)
      this.title = "添加工艺路线";
      this.optType = "add";
    },
    // 查询明细按钮操作
    handleView(row) {
      this.reset();
      const routeId = row.routeId || this.ids;
      getProrouteBom(routeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.getProcess(routeId);
        this.title = "查看工艺线路信息";
        this.optType = "view";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const routeId = row.routeId || this.ids
      getProrouteBom(routeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.getProcess(routeId);
        this.title = "修改工艺路线";
        this.optType = "edit";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.routeId != null) {
            updateProrouteBom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.attr3 = 1;
            addProrouteBom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const routeIds = row.routeId || this.ids;
      this.$modal.confirm('是否确认删除工艺路线编号为"' + row.routeCode + '"的数据项？').then(function () {
        return delProrouteBom(routeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pro/proroute/export', {
        ...this.queryParams
      }, `proroute_${new Date().getTime()}.xlsx`)
    },
    //自动生成编码
    handleAutoGenChange(autoGenFlag) {
      if (autoGenFlag) {
        genCode('ROUTE_CODE').then(response => {
          this.form.routeCode = response;
        });
      } else {
        this.form.routeCode = null;
      }
    },
    //获取当前产品对应的生产工序
    getProcess(routeId) {
      listProcess(routeId).then(response => {
        this.processOptions = response.data;
      });
    },
    handleSelectBom() {
      this.$refs.bomSelect.showFlag = true;
    },
    //物料选择弹出框
    onBomSelected(obj) {
      if (obj != undefined && obj != null) {
        this.form.bomId = obj.bomId
        this.form.bomCode = obj.bomCode
        this.form.bomName = obj.bomName
        this.form.productCode = obj.itemCode
        this.form.productName = obj.itemName
        this.form.attr1 = obj.drawNumber
        this.form.drawingId = ''
        this.form.drawingName = ''
        this.form.drawingVersion = ''

        this.queryParam = {
          itemCode: obj.itemCode,
          status: 1,
          processAuthorization: 1,
        }
        listDrawing(this.queryParam).then(response => {
          if (response.rows != null && response.rows.length > 0) {
            this.form.drawingId = response.rows[0].id
            this.form.drawingName = response.rows[0].drawingName
            this.form.drawingVersion = response.rows[0].version
          }
        });
      }
    },
    handleSelectDrawing() {
      this.$refs.drawingSelect.showFlag = true;
    },
    //物料选择弹出框
    onDrawingSelected(obj) {
      if (obj != undefined && obj != null) {
        this.form.drawingId = obj.id
        this.form.drawingName = obj.drawingName
        this.form.drawingVersion = obj.version
        this.form.attr1 = obj.drawNumber
      }
    },
    handleInput(newValue) {
      this.form.status = newValue;
      this.getProcess(this.form.routeId)
    },
    //状态修改
    handleStatusChange(row) {
      this.$modal.confirm('确定要确认工艺流程吗?确认后工序将无法更改！').then(function () {
        return changeRouteStatus(row.routeId, 'Y');
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
      });
    },
    //状态修改
    handleEnableChange(row) {
      let text = row.enableStatus === "Y" ? "启用" : "禁用";
      this.$modal.confirm('确认要"' + text + '""' + row.routeName + '"吗？').then(function () {
        return changeEnableStatus(row.routeId, row.enableStatus);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.enableStatus = row.enableStatus === "Y" ? "N" : "Y";
      });
    },
    /** 复制按钮操作 */
    handleCopy(row) {
      const routeIds = row.routeId || this.ids;
      this.$modal.confirm('是否确认复制工艺路线编号为"' + row.routeCode + '"的数据项？').then(function () {
        return copyProrouteBom(routeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("复制成功");
      }).catch(() => {
      });
    },
  }
};
</script>
